<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visibleDialog"
    :title="props.title"
    :width="!props.isReadonly ? '900px' : '900px'"
    draggable
    @close="closeDialog1"
  >
    <div style="width: 900px; display: flex; justify-content: center">
      <!-- <el-steps style="width: 650px" :active="1">
        <el-step title="1" description="未审核" />
        <el-step title="2" description="提交审核" />
        <el-step title="3" description="已发布" />
      </el-steps> -->
      <el-steps style="width: 650px" :active="1" finish-status="success">
        <el-step title="未审核" />
        <el-step title="提交审核" />
        <el-step title="已发布" />
      </el-steps>
    </div>
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100"
      style="margin-top: 20px"
      v-show="!props.isReadonly"
    >
      <el-row>
        <el-col :span="8">
          <el-form-item label="问题编号" prop="qualityProblemCode">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.qualityProblemCode"
              placeholder="请输入问题编号"
              clearable
            />
            <span v-else>{{ formData.qualityProblemCode }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8"></el-col>
        <el-col :span="8"></el-col>
        <el-col :span="8">
          <el-form-item label="提出部门" prop="proposeDepartmentName">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.proposeDepartmentName"
              placeholder="请输入提出部门"
              clearable
            />
            <span v-else>{{ formData.proposeDepartmentName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="提出时间" prop="createTime" style="width: 100%">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.createTime"
              placeholder="请输入提出时间"
              clearable
            />
            <span v-else>{{ formData.createTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="提出人" prop="createdUserName">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.createdUserName"
              placeholder="请输入提出人"
              clearable
            />
            <span v-else>{{ formData.createdUserName }}</span>
          </el-form-item>
        </el-col>

        <!-- <el-col :span="8">
          <el-form-item label="部门经理" prop="auditUserName">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.auditUserName"
              placeholder="请输入部门经理"
              clearable
            />
            <span v-else>{{ formData.auditUserName }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="经理审核时间" prop="approvalTime">
            <el-input
              disabled
              v-if="!props.isReadonly"
              v-model="formData.approvalTime"
              placeholder="请输入经理审核时间"
              clearable
            />
            <span v-else>{{ formData.approvalTime }}</span>
          </el-form-item>
        </el-col> -->
        <!-- <el-col :span="8"></el-col> -->
        <el-col :span="8">
          <el-form-item label="责任部门" prop="departmentId">
            <el-tree-select
              v-if="!props.isReadonly"
              placeholder="请选择责任部门"
              v-model="formData.departmentId"
              :data="organizationList"
              :render-after-expand="false"
              :check-strictly="true"
              node-key="id"
              :props="{
                label: 'organizationCode',
                value: 'id',
                children: 'nodes',
              }"
              style="width: 100%"
              @node-click="handleChangeDepartment"
              clearable
            />
            <span v-else>{{ formData.departmentId }}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="责任人" prop="departmentManagerId">
            <el-select
              style="width: 100%"
              v-if="!props.isReadonly"
              v-model="formData.departmentManagerId"
              placeholder="请选择责任人"
              clearable
              @change="handleChangePerson"
            >
              <el-option
                v-for="item in personList"
                :label="`${item.firstName}`"
                :value="`${item.id}`"
              />
            </el-select>
            <span v-else>{{ formData.approvalTime }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <div style="display: flex; align-items: center; margin-left: 40px">
            限
            <el-date-picker
              v-model="formData.provisionalMeasuresDeadline"
              :rules="rules.provisionalMeasuresDeadline"
              type="date"
              placeholder="请输入时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />前上报纠正预防措施
          </div>
        </el-col>
        <el-col :span="24">
          <div
            style="
              display: flex;
              align-items: center;
              margin-top: 20px;
              margin-left: 40px;
            "
          >
            限
            <el-date-picker
              v-model="formData.rectificationDeadline"
              type="date"
              :rules="rules.rectificationDeadline"
              placeholder="请输入时间"
              format="YYYY-MM-DD"
              value-format="YYYY-MM-DD"
            />前上报纠正前整改完毕
          </div>
        </el-col>
        <el-col :span="24">
          <div style="display: flex; flex-direction: column; margin-top: 20px">
            存在（或潜在）问题及整改要求：
            <el-input
              :autosize="{ minRows: 6, maxRows: 6 }"
              v-model="formData.rectificationRequirement"
              :rules="rules.rectificationRequirement"
              maxlength="255"
              style="width: 850px"
              placeholder="请输入"
              show-word-limit
              type="textarea"
            />
          </div>
        </el-col>
      </el-row>
    </el-form>

    <template #footer>
      <div class="dialog-footer">
        <el-button
          v-if="!props.isReadonly"
          type="primary"
          :loading="loading"
          @click="backStep"
          >上一步</el-button
        >
        <el-button
          v-if="!props.isReadonly"
          :loading="loading"
          type="primary"
          @click="handleSubmit"
          >提交</el-button
        >

        <el-button @click="closeDialog">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import dayjs from "dayjs";
// import html2pdf from "./PDF.js";
const dictionaryStore = useDictStoreHook();

function PDFprit() {
  const table = document.getElementById("table");
  // html2pdf(table, {
  //   background: "#fff",
  //   minimumUnit: "tr",
  //   orientation: "l",
  // });
  // html2pdf("table", "对账单", dayjs(new Date()).format("YYYY-mm-dd"));
}
defineOptions({
  name: "Q8DRELEASEITEMEDIT",
  inheritAttrs: false,
});

const emit = defineEmits([
  "reset-table",
  "change-visible1",
  "backList",
  "goback",
]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;

    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
  qualityExceptionCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  productionTypeId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productionTypeName: [
    // { required: true, message: "请选择产品类目名称", trigger: "change" },
  ],
  materialId: [
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  materialCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  materialName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  figureNumber: [
    // { required: true, message: "请选择图号", trigger: "change" },
  ],
  qualityQuantity: [
    // { required: true, message: "请输入数量", trigger: "blur" },
  ],
  failureQuantity: [
    // { required: true, message: "请输入失效对应数量", trigger: "blur" },
  ],
  failureMode: [
    // { required: true, message: "请选择失效模式", trigger: "change" },
  ],
  occurrenceTime: [
    // { required: true, message: "请选择发生时间", trigger: "change" },
  ],
  occurrenceLocation: [
    // { required: true, message: "请选择发生地点", trigger: "change" },
  ],
  informationSourceId: [
    // { required: true, message: "请选择信息来源", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  informationSourceContent: [
    { min: 1, max: 255, message: "长度在 1 到 255 个字符", trigger: "blur" },
  ],
  exceptionState: [],
  isInternal: [],
  supplyId: [
    // { required: true, message: "请选择生产单位", trigger: "change" },
    { min: 1, max: 32, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  supplyCode: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  supplyName: [
    { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" },
  ],
  problemTypeCode: [
    // { required: true, message: "请选择问题分类", trigger: "change" },
  ],
  lineProcessName: [
    // { required: true, message: "请输入生产线/过程", trigger: "blur" },
    { min: 1, max: 50, message: "长度在 1 到 32 个字符", trigger: "blur" },
  ],
  productProcess: [
    // { required: true, message: "请选择产品/过程状态", trigger: "change" },
  ],
  problemFrequency: [
    // { required: true, message: "请输入问题发生频度", trigger: "blur" },
  ],
  //   lineProcessName: [
  //     // { required: true, message: "请选择责任部门", trigger: "change" },
  //   ],
  departmentId: [
    { required: true, message: "请选择责任部门", trigger: "change" },
  ],
  departmentManagerId: [
    { required: true, message: "请选择责任人", trigger: "change" },
  ],
  rectificationRequirement: [
    { required: true, message: "请输入", trigger: "change" },
  ],
  rectificationDeadline: [
    { required: true, message: "请输入", trigger: "change" },
  ],
  provisionalMeasuresDeadline: [
    { required: true, message: "请输入", trigger: "blur" },
  ],
});

const formRef = ref(ElForm);

let formData = reactive({
  id: "",
  qualityProblemCode: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  informationSourceId: "",
  lineProcessName: "",
  productProjectName: "",
  sortCodeId: "",
  sortCode: "",
  productProcess: "",
  failureMode: "",
  consequenceInfluence: "",
  problemFrequency: "",
  proposeDepartmentId: "",
  proposeDepartmentCode: "",
  proposeDepartmentName: "",
  proposeManagerId: "",
  proposeManagerName: "",
  departmentId: "",
  departmentCode: "",
  departmentName: "",
  departmentManagerId: "",
  departmentManagerName: "",
  isInternal: 1,
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  problemTime: "",
  problemUserId: "",
  problemUserName: "",
  problemTypeCode: "",
  exceptionTypeSubId: "",
  exceptionTypeSubCode: "",
  exceptionTypeSubName: "",
  exceptionLevel: "",
  problemApproachId: "",
  problemApproachCode: "",
  problemApproachType: "",
  problemApproachName: "",
  releaseState: "",
  auditUserId: "",
  auditUserName: "",
  provisionalMeasuresDeadline: "",
  rectificationDeadline: "",
  rectificationRequirement: "",
  agreeWith: "",
  approvalOpinion: "",
  approvalTime: "",
  targetTime: "",
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
  // 涉及数量
  qualityQuantity: 1,
  // 失效模式
  failureModeContent: "",
  // 发生地点
  occurrenceLocation: "",
  // 状态
  productProcessContent: "",
  sortName: "",
  // 图号编号
  materialProcessCode: "",
  materialProcessName: "",
  materialProcessId: "",
  materialProcessFigure: "",
  // 问题处理方式
  exceptionSolvingType: "",
  // 问题级别
  exceptionLevelCode: "",
  exceptionLevelName: "",
  exceptionLevelContent: "",
  exceptionLevelId: "",
  // 8D问题反馈报告
  q8dExceptionAssignDTO: [
    {
      id: "",
      qualityExceptionId: "",
      qualityExceptionCode: "",
      exceptionAssignCode: "",
      customerComplaintCode: "",
      startDate: "",
      contentTypeId: "",
      contentType: "",
      propertySortId: "",
      propertySort: "",
      supplyId: "",
      supplyCode: "",
      supplyName: "",
      materialId: "",
      materialCode: "",
      materialName: "",
      figureNumber: "",
      exceptionQuantity: 1,
      exceptionDescription: "",
      exceptionAssignState: "",
      assetPhotos: [],
      version: "",
      remarks: "",
      createTime: "",
      createdUserId: "",
      createdUserName: "",
      lastUpdateTime: "",
      lastUpdatedUserId: "",
      lastUpdatedUserName: "",
      enabled: "",
      deleted: "",
    },
  ],
});

watch(
  () => props.data,
  (newVal: any) => {
    if (newVal?.id) {
      const {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevel,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        // 图号编号
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
        exceptionSolvingType,
        exceptionLevelCode,
        exceptionLevelName,
        exceptionLevelContent,
        q8dExceptionAssignDTO,
        exceptionLevelId,
      } = newVal;
      Object.assign(formData, {
        id,
        qualityProblemCode,
        qualityExceptionId,
        qualityExceptionCode,
        informationSourceId,
        lineProcessName,
        productProjectName,
        sortCodeId,
        sortCode,
        productProcess,
        failureMode,
        consequenceInfluence,
        problemFrequency,
        proposeDepartmentId,
        proposeDepartmentCode,
        proposeDepartmentName,
        proposeManagerId,
        proposeManagerName,
        departmentId,
        departmentCode,
        departmentName,
        departmentManagerId,
        departmentManagerName,
        isInternal,
        supplyId,
        supplyCode,
        supplyName,
        problemTime,
        problemUserId,
        problemUserName,
        problemTypeCode,
        exceptionTypeSubId,
        exceptionTypeSubCode,
        exceptionTypeSubName,
        exceptionLevel,
        problemApproachId,
        problemApproachCode,
        problemApproachType,
        problemApproachName,
        releaseState,
        auditUserId,
        auditUserName,
        provisionalMeasuresDeadline,
        rectificationDeadline,
        rectificationRequirement,
        agreeWith,
        approvalOpinion,
        approvalTime,
        targetTime,
        version,
        remarks,
        createTime,
        createdUserId,
        createdUserName,
        lastUpdateTime,
        lastUpdatedUserId,
        lastUpdatedUserName,
        enabled,
        deleted,
        qualityQuantity,
        failureModeContent,
        occurrenceLocation,
        productProcessContent,
        sortName,
        materialProcessCode,
        materialProcessName,
        materialProcessId,
        materialProcessFigure,
        exceptionSolvingType,
        exceptionLevelCode,
        exceptionLevelName,
        exceptionLevelContent,
        q8dExceptionAssignDTO: q8dExceptionAssignDTO ?? [],
        exceptionLevelId,
      });

      if (formData.problemTypeCode == "1") {
        problemApproachTypeList.value = [{ value: "0", label: "预防纠正措施" }];
      } else {
        problemApproachTypeList.value = [
          { value: "1", label: "简单问题处理" },
          { value: "2", label: "问题8D处理" },
        ];
      }
      getPersonList(formData.departmentId);
      console.log("formData", formData);
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible1", false);
  resetForm();
}
/** 关闭表单弹窗 */
function closeDialog1() {
  // 如果使上一步 就把from传过去 不然就使关闭弹窗和取消按钮不弹窗
  if (backBtn.value) {
    visibleDialog.value = false;
    emit("backList", false, formData);
    backBtn.value = false;
  } else {
    visibleDialog.value = false;
    emit("change-visible1", false);
    resetForm();
  }
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.id = "";
  formData.qualityProblemCode = "";
  formData.qualityExceptionId = "";
  formData.qualityExceptionCode = "";
  formData.informationSourceId = "";
  formData.lineProcessName = "";
  formData.productProjectName = "";
  formData.sortCodeId = "";
  formData.sortCode = "";
  formData.productProcess = "";
  formData.failureMode = "";
  formData.consequenceInfluence = "";
  formData.problemFrequency = "";
  formData.proposeDepartmentId = "";
  formData.proposeDepartmentCode = "";
  formData.proposeDepartmentName = "";
  formData.proposeManagerId = "";
  formData.proposeManagerName = "";
  formData.departmentId = "";
  formData.departmentCode = "";
  formData.departmentName = "";
  formData.departmentManagerId = "";
  formData.departmentManagerName = "";
  formData.isInternal = 1;
  formData.supplyId = "";
  formData.supplyCode = "";
  formData.supplyName = "";
  formData.problemTime = "";
  formData.problemUserId = "";
  formData.problemUserName = "";
  formData.problemTypeCode = "";
  formData.exceptionTypeSubId = "";
  formData.exceptionTypeSubCode = "";
  formData.exceptionTypeSubName = "";
  formData.exceptionLevel = "";
  formData.problemApproachId = "";
  formData.problemApproachCode = "";
  formData.problemApproachType = "";
  formData.problemApproachName = "";
  formData.releaseState = "";
  formData.auditUserId = "";
  formData.auditUserName = "";
  formData.provisionalMeasuresDeadline = "";
  formData.rectificationDeadline = "";
  formData.rectificationRequirement = "";
  formData.agreeWith = "";
  formData.approvalOpinion = "";
  formData.approvalTime = "";
  formData.targetTime = "";
  formData.version = "";
  formData.remarks = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
  formData.enabled = "";
  formData.deleted = "";
  formData.qualityQuantity = 1;
  formData.occurrenceLocation = "";
  formData.productProcessContent = "";
  formData.sortName = "";
  formData.materialProcessCode = "";
  formData.materialProcessName = "";
  formData.materialProcessId = "";
  formData.materialProcessFigure = "";
  formData.exceptionSolvingType = "";
  formData.exceptionLevelCode = "";
  formData.exceptionLevelName = "";
  formData.exceptionLevelContent = "";
  formData.exceptionLevelId = "";
  formData.q8dExceptionAssignDTO = [
    JSON.parse(JSON.stringify(q8dExceptionAssignDTO)),
  ];
}
const q8dExceptionAssignDTO = {
  id: "",
  qualityExceptionId: "",
  qualityExceptionCode: "",
  exceptionAssignCode: "",
  customerComplaintCode: "",
  startDate: "",
  contentTypeId: "",
  contentType: "",
  propertySortId: "",
  propertySort: "",
  supplyId: "",
  supplyCode: "",
  supplyName: "",
  materialId: "",
  materialCode: "",
  materialName: "",
  figureNumber: "",
  exceptionQuantity: 1,
  exceptionDescription: "",
  exceptionAssignState: "",
  assetPhotos: [],
  version: "",
  remarks: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  enabled: "",
  deleted: "",
};
const loading = ref(false);

/** 质量异常信息提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      console.log(formData);
      console.log(
        formData.provisionalMeasuresDeadline,
        formData.rectificationDeadline
      );

      if (
        formData.provisionalMeasuresDeadline == undefined ||
        formData.rectificationDeadline == undefined
      ) {
        ElMessage.error("请选择上报和整改完毕时间！");
        return;
      } else {
        if (
          formData.provisionalMeasuresDeadline > formData.rectificationDeadline
        ) {
          ElMessage.error("请选择上报不能大于整改完毕时间！");
          return;
        }
      }
      if (formData.rectificationRequirement == "") {
        ElMessage.error("请填写 存在（或潜在）问题及整改要求！");
        return;
      }
      loading.value = true;
      const text = formData.id ? "提交" : "提交";
      await request({
        url: "/qms/q8dqualityproblem/publish",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("goback", false);
      emit("reset-table", formData.id);
    }
  });
}

// // 产品过程状态列表
// const materialProcessStateList = ref([]);
// const getMaterialProcessStateList = async () => {
//   let res = await request({
//     url: "/qms/qmsmaterialprocessstate/getList",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   console.log("产品状态", res.data);

//   materialProcessStateList.value = res.data;
// };

// // 改变产品状态
// const handleChangeMaterialProcessState = (e) => {
//   if (e) {
//     console.log(e);
//   }
// };

// // 供应商列表
// const supplyList = ref([]);
// async function getSupplyList() {
//   let res = await request({
//     url: "mdm/mdmpartner/getListByFull",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   supplyList.value = res.data;
// }

// const changSupplyList = (e) => {
//   if (e) {
//     supplyList.value.forEach((item) => {
//       if (item.id == e) {
//         formData.supplyName = item.partnerName;
//         formData.supplyCode = item.partnerCode;
//       }
//     });
//   } else {
//     formData.supplyName = "";
//     formData.supplyCode = "";
//   }
// };

// const changIsInternal = (e) => {
//   console.log(e);
//   formData.supplyId = "";
//   formData.supplyName = "";
//   formData.supplyCode = "";
// };

// 责任部门

const organizationList = ref([]);
const getOrganizationList = async () => {
  let res = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 4 },
  }).finally(() => {
    loading.value = false;
  });
  organizationList.value = res.data;
  console.log("organizationList", organizationList.value);
};

const handleChangeDepartment = (e) => {
  if (e) {
    personList.value = [];
    formData.departmentManagerId = "";
    getPersonList(e.id);
    formData.departmentName = e.organizationName;
    formData.departmentCode = e.organizationCode;
  } else {
    personList.value = [];
    formData.departmentManagerId = "";
    formData.departmentName = "";
    formData.departmentCode = "";
    formData.departmentManagerName = "";
  }
};

// 责任人
const personList = ref([]);
const getPersonList = async (id) => {
  const res = await request({
    url: "/system/sysUser/getList ",
    method: "post",
    data: {
      departmentId: id,
    },
  });
  if (res.data) {
    personList.value = res.data;
    console.log("personList", personList.value);
  }
};
// 改变责任人
const handleChangePerson = (e) => {
  if (e) {
    personList.value.forEach((item) => {
      if (item.id == e) {
        formData.departmentManagerName = item.firstName;
      }
    });
  } else {
    formData.departmentManagerName = "";
  }
};

// // 失效模式树列表
// const failureModeList = ref([]);
// async function getFailureMode() {
//   let res = await request({
//     url: "/qms/qmsfailuremode/getTree",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   failureModeList.value = res.data;
//   console.log("failureMode", failureModeList.value);
// }

// const handleNodeClick = (e) => {
//   if (e) {
//     formData.failureModeContent = e.failureModeContent;
//   } else {
//     formData.failureModeContent = "";
//   }
// };

// // 图号编号列表
// const materialProcessList = ref([]);
// async function getMaterialProcess() {
//   let res = await request({
//     url: "/qms/qmsmaterialprocess/getList",
//     method: "post",
//     data: {},
//   }).finally(() => {
//     loading.value = false;
//   });
//   materialProcessList.value = res.data;
//   console.log("materialProcessList", materialProcessList.value);
// }
// const changeFigureNumber = (e) => {
//   if (e) {
//     materialProcessList.value.forEach((item) => {
//       if (item.id == e) {
//         formData.materialProcessCode = item.materialProcessCode;
//         formData.materialProcessName = item.materialProcessName;
//         formData.materialProcessFigure = item.materialProcessFigure;
//       }
//     });
//   } else {
//     formData.materialProcessCode = "";
//     formData.materialProcessName = "";
//     formData.materialProcessFigure = "";
//   }
// };

// 问题级别
const problemLevelList = ref([]);
const getProblemLevelList = async () => {
  let res = await request({
    url: "/qms/qmsexceptionlevel/getList",
    method: "post",
    data: {},
  }).finally(() => {
    loading.value = false;
  });
  problemLevelList.value = res.data;
  console.log("problemLevelList", problemLevelList.value);
};

// // 选择问题级别
// const handleChangeProblemLevel=(e)=>{
//   problemLevelList.value.forEach((item)=>{
//     if(item.id==e){
//       formData.exceptionLevel=item.id
//     }
//   })
// }
// 改变问题分类获取问题处理方式类型
const problemApproachTypeList = ref([]);
const problemTypeChange = () => {
  if (formData.problemTypeCode == "1") {
    problemApproachTypeList.value = [{ value: "0", label: "预防纠正措施" }];
  } else {
    problemApproachTypeList.value = [
      { value: "1", label: "简单问题处理" },
      { value: "2", label: "问题8D处理" },
    ];
  }
};

// 上一步
let backBtn = ref(false);

const backStep = () => {
  // 点击上一步时使 backBtn.value=true弹窗自动关闭只调用一次
  backBtn.value = true;
  visibleDialog.value = false;
  // emit("change-visible1", false, formData);
};

onMounted(() => {
  // getFailureMode();
  // getMaterialProcessStateList();
  getOrganizationList();
  // getSupplyList();
  // getMaterialProcess();
  getProblemLevelList();
});
// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible1", false);
});
</script>
<style></style>
